//
//  JiangLijinViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/14.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "JiangLijinViewController.h"
#import "JTCSM.h"
#import "JiangLiJinTableViewCell.h"
#import "JiangLiJinModel.h"
static CGFloat iamgeviewsize = 200;
static CGFloat space = 20;
@interface JiangLijinViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *titlearray;
    UITableView *tableview;
    UILabel *titleLabel;
    NSString *page;
}
@property (nonatomic,strong) UILabel *label;

@end

@implementation JiangLijinViewController

- (void)gotobackVC{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    [df isRemoveTarbar:YES];
    page = @"";
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,KCONTROLLERWith , iamgeviewsize)];
    imageview.image = [UIImage imageNamed:@"bg_banner4@2x.png"];
    [self.view addSubview:imageview];
    imageview.userInteractionEnabled = YES;
    _label = [UILabel initWithFrame:CGRectMake(0, 30, KSCREENWidth, 44) text:@"王者管家奖励金" font:22 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:[UIColor clearColor]];
    [self.view addSubview:_label];
    
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(10, 30, 60, 44);
    [button setImage:[UIImage imageNamed:@"return_iv_white.png"] forState:UIControlStateNormal];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    //        image的位置大小调整
    button.imageEdgeInsets = UIEdgeInsetsMake(12.5, 5, 12.5, 40);
    //        title的位置大小调整
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    [button addTarget:self action:@selector(gotobackVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    
    titleLabel = [UILabel initWithFrame:CGRectMake(40, KagainChangeHeight+20, KCONTROLLERWith-80, 44) text:@"分享新用户注册链接，对方成功激活后.\n您的账户将获得20元奖励金" font:15 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:nil];
    titleLabel.numberOfLines = 2;
    [self.view addSubview:titleLabel];
    [self layoutTableView];
    [self therequest];
       // Do any additional setup after loading the view.
}

- (void)layoutTableView{
    
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(space, KVIEW_Y(titleLabel)+20, KCONTROLLERWith-2*space, 249) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [df layerWithView:tableview radius:4 width:0 color:nil];
   [df setTheViewYiYing:tableview shadowColor:RGBACOLOR(241, 241, 241, 1) shadowOffset:CGSizeMake(3, 3) apla:0.5];
    [self setUpTableView];
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(tableview)+8, KCONTROLLERWith, KCONTROLLERHeight)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    
}

- (void)setUpTableView{
    // 上拉加载
    [tableview addFooterWithTarget:self action:@selector(footerRereshing)];
    
}

- (void)footerRereshing{
    // 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KRefreshTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //getMarriageSeeking(网络请求)
        [self loadMoreDataRequest];
        
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return titlearray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
    NSString *cellid = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    JiangLiJinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[JiangLiJinTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
 
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [JiangLiJinTableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableview];
}
- (void)therequest{
//    UserBill/bonus
    [titlearray removeAllObjects];
    titlearray = nil;
    titlearray =[NSMutableArray array];
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"UserBill/bonus") withDict:@{@"token":KTokenString} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            NSDictionary *dict = KResultOfKey(@"data");
            NSArray *bannerArr = [dict objectForKey:@"list"];
            page = [dict objectForKey:@"p"];
            for (NSDictionary *bannerdict in bannerArr) {
                JiangLiJinModel *model = [JiangLiJinModel whc_ModelWithJson:bannerdict];
                [titlearray addObject:model];
            }
            if (titlearray == nil || titlearray.count == 0) {
                [[MyAlertView shareSingle] showWithImagename:@"zhangbenimage.png" contentLabel:@"你还没有奖励金 \n 赶快去邀请小伙伴加入吧" superview:tableview];
            }else{
                [[MyAlertView shareSingle] hiddenmyself];
            }
            [tableview reloadData];
            [tableview footerEndRefreshing];
        }
    }];

}
- (void)loadMoreDataRequest{
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"UserBill/bonus") withDict:@{@"token":KTokenString,@"p":page} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            NSDictionary *dict = KResultOfKey(@"data");
            NSArray *bannerArr = [dict objectForKey:@"list"];
            page = [dict objectForKey:@"p"];
            for (NSDictionary *bannerdict in bannerArr) {
                JiangLiJinModel *model = [JiangLiJinModel whc_ModelWithJson:bannerdict];
                [titlearray addObject:model];
            }
            if (titlearray == nil || titlearray.count == 0) {
                [[MyAlertView shareSingle] showWithImagename:@"zhangbenimage.png" contentLabel:@"你还没有奖励金 \n 赶快去邀请小伙伴加入吧" superview:tableview];
            }else{
                [[MyAlertView shareSingle] hiddenmyself];
            }
            [tableview reloadData];
            [tableview footerEndRefreshing];
        }
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
